package a10_动态规划;

/**
 * <p>
 * a47_判断子序列
 * </p>
 *
 * @author flyduck
 * @since 2025/2/28
 */
public class a47_判断子序列 {
    public static void main(String[] args) {
        a47_判断子序列 test = new a47_判断子序列();
        System.out.println(test.isSubsequence("abc", "ahbgdc"));
    }
    //dp[i][j]:以char1[i-1]结尾的数组和以char2[j-1]结尾的数组的最长公共子序列的长度为dp[i][j]

    //递推公式：
    // if(char1[i-1] == char2[j-1]){
    //     dp[i][j] = dp[i-1][j-1];
    // }else{
    //     dp[i][j] = dp[i][j-1];
    // }

    //初始化
    public boolean isSubsequence(String s, String t) {
        char[] chars1 = s.toCharArray();
        char[] chars2 = t.toCharArray();

        int[][] dp = new int[chars1.length+1][chars2.length+1];

        for (int i = 1; i <= chars1.length; i++) {
            for (int j = 1; j <= chars2.length; j++) {
                if(chars1[i-1] == chars2[j-1]){
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else {
                    dp[i][j] = dp[i][j-1];
                }
            }
        }

        return dp[chars1.length][chars2.length] == s.length();
    }
}
